package Classes;
import java.io.*;


/**
 *
 * @author ward
 */
public class OutputXml {
    
    static public void RunExecutedProjectToXml( PrintWriter out, String projectFileName, AE.Data analysisProject )
    {
        out.print( "<Project>" );

        if( projectFileName != null )
        {
            out.println( "<ProjectFileName>" );
            out.println( projectFileName ) ;
            out.println( "</ProjectFileName>" );
        }            

        out.print( "<Pages>" );
        for( int currentVisiblePageIndex = 0,  visiblePageCount = analysisProject.GetPageCount() ;
                currentVisiblePageIndex < visiblePageCount ;
                currentVisiblePageIndex++ )
        {
            analysisProject.SetSelectedPage( currentVisiblePageIndex );
            AE.Page currentPage = analysisProject.GetSelectedPage();
            if( currentPage.Visible() )
            {
                out.print( "<Page>" );
                
                out.println( "<PageCaption>");
                out.println( currentPage.GetPageCaption() );
                out.println( "</PageCaption>");
                
                out.println( "<PageDescription>");
                out.println( currentPage.GetPageDescription() );
                out.println( "</PageDescription>" );
                
                GenXmlToPrintWriter( out, currentPage );
                
                out.print( "</Page>" );
            }
        }
        out.print( "</Pages>" );
        out.println( "</Project>" );
    }
    
    static private void GenXmlToPrintWriter( PrintWriter out, AE.Page currentPage )
    {
        out.println( "<Header>" );

        out.println( "<RowLabelCaption>" );
        out.println( currentPage.GetRowLabelName() );
        out.println( "</RowLabelCaption>" );

        int   textCount = currentPage.GetTextColumnCount() ;
        if( textCount > 0 )
        {
            out.println( "<TextColumnNames>"  );
            for( int textColumnIndex = 0;
                    textColumnIndex < textCount ; textColumnIndex++ )
            {
                out.println( "<TextColumnName>"  );
                out.println( currentPage.GetTextColumnName( textColumnIndex ) );
                out.println( "</TextColumnName>" );
            }
            out.println( "</TextColumnNames>" );
        }

        int columnCount = currentPage.GetNumericColumnCount() ;
        if( columnCount > 0 )
        {
            out.println( "<NumericColumnNames>"  );
            for( int numericColumnIndex = 0;
                    numericColumnIndex < columnCount ; numericColumnIndex++ )
            {
                out.println( "<NumericColumnName>"  );
                out.println( currentPage.GetNumericColumnName(numericColumnIndex ) );
                out.println( "</NumericColumnName>" );
            }
            out.println( "</NumericColumnNames>" );
        }
        out.println( "</Header>" );

        int rowCount = currentPage.GetRowCount() ;
        if( rowCount > 0 )
        {
            out.println( "<Rows>" );
            for( int rowIndex = 0 ; rowIndex < rowCount ; rowIndex++ )
            {
                out.println( "<Row>" );
                out.println( "<RowLabelValue>" );
                out.println( currentPage.GetRowLabelValue( rowIndex ) );
                out.println( "</RowLabelValue>" );

                if( textCount > 0 )
                {
                    out.println( "<TextColumnValues>"  );
                    for( int textColumnIndex = 0;
                            textColumnIndex < textCount ; textColumnIndex++ )
                    {
                        out.println( "<TextColumnValue>"  );
                        out.println( currentPage.GetTextColumnValue( rowIndex, textColumnIndex ) );
                        out.println( "</TextColumnValue>"  );
                    }
                    out.println( "</TextColumnValues>"  );
                }

                if( columnCount > 0 )
                {
                    out.println( "<NumericColumnValues>"  );
                    for( int numericColumnIndex = 0;
                            numericColumnIndex < columnCount ; numericColumnIndex++ )
                    {
                        String alertLevel = currentPage.GetAlertLevel( rowIndex, numericColumnIndex );
                        out.println( "<NumericColumnAlert>" ) ;
                        out.println( alertLevel );
                        out.println( "</NumericColumnAlert>" ) ;

                        out.println( "<NumericColumnFormattedValue>"  );
                        out.println( currentPage.GetNumericCellContents( rowIndex, numericColumnIndex, true ) );
                        out.println( "</NumericColumnFormattedValue>"  );
                        
                        out.println( "<NumericColumnFormattedValue>"  );
                        out.println( currentPage.GetNumericCellContents( rowIndex, numericColumnIndex, false ) );
                        out.println( "</NumericColumnFormattedValue>"  );
                    }
                    out.println( "</NumericColumnValues>"  );
                }
                out.println( "</Row>" );
            }
            out.println( "</Rows>" );
        }
    }

}
